// 工厂类
class FlyweightFactory {
    constructor() {
        this.flyweights = {}
    }
    getFlyweight (key) {
        if (!this.flyweights[key]) {
            this.flyweights[key] = new ConcreteFlyweight(key)
        }
        return this.flyweights[key]
    }
}

// 具体享元类
class ConcreteFlyweight {
    constructor(key) {
        this.key = key
    }
    operation () {
        console.log(`${this.key}执行操作`)
    }
}

// 使用享元类
const factory = new FlyweightFactory()
const flyweight1 = factory.getFlyweight('key')
const flyweight2 = factory.getFlyweight('key')
flyweight1.operation()
flyweight2.operation()
console.log(flyweight1 === flyweight2)
